﻿// <copyright file="Global.asax.cs">
//  Copyright (c) 2008 Eric Patey
//
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

namespace WaringAuction
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using EricUtilities;
    using EricUtilities.Azure;
    using Microsoft.Samples.ServiceHosting.StorageClient;
    using WaringAuction.Models;

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            ////routes.MapRoute(
            ////    "Home",                                              // Route name
            ////    "",                                                 // URL with parameters
            ////    new { controller = "Home", action = "Index" }  // Parameter defaults
            ////);

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = string.Empty }); // Parameter defaults
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Called by Mvc framework as instance method.")]
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This method is called by the Mvc framework through reflection.")]
        private static void AzureStorageInitialization(HttpContext context)
        {
            // This is where you put initialization logic for the site.
            // RoleManager is properly initialized at this point.

            ////if (!Roles.RoleExists("Administrators")) Roles.CreateRole("Administrators");
            ////if (!Roles.RoleExists("Friends")) Roles.CreateRole("Friends");
            ////if (Membership.GetUser("admin") == null)
            ////{
            ////    Membership.CreateUser("admin", "admin", "admin@mypersonalsite.com");
            ////    Roles.AddUserToRole("admin", "Administrators");
            ////}

            StorageAccountInfo tableAccountInfo = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
            context.Application["tableAccountInfo"] = tableAccountInfo;

            StorageInitialization.OnetimeInitialization(tableAccountInfo);
            TableStorage.CreateTablesFromModel(typeof(AuctionDataServiceContext), tableAccountInfo);

            StorageAccountInfo blobAccountInfo = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
            BlobStorage blobStorage = BlobStorage.Create(blobAccountInfo);
            BlobContainer blobContainer = blobStorage.GetBlobContainer("images");
            blobContainer.CreateContainer(null, ContainerAccessControl.Public);

            context.Application["blobStorage"] = blobStorage;
            context.Application["blobContainer"] = blobContainer;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This method is called by the Mvc framework through reflection.")]
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            SingletonInitialization.Initialize(delegate { AzureStorageInitialization(((HttpApplication)sender).Context); });
        }
    }
}